package com.ac.app.web.action.invest;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;

import com.ac.app.dm.PickInvest;
import com.ac.app.web.form.PickInvestSearchForm;
import com.ac.app.web.view.PickInvestView;
import com.caac.core.config.OrderClazz;
import com.caac.core.config.QueryClass;
import com.caac.core.dao.CriteriaCallback;
import com.caac.core.dm.OrderProp;
import com.caac.core.util.Page;
import com.caac.core.web.action.BasePageViewAction;
import com.caac.core.web.action.PageViewForward;
import com.caac.core.web.form.BaseForm;

public class SearchPickInvestAction extends BasePageViewAction {
	private static final String QUERY_NAME = "pkInvestQuery";
	private static final String ORDER_NAME = "pkInvestOrder";
	private static final String VIEW_NAME = "pickInvestView";

	protected PageViewForward doPageViewExecute(ActionMapping mapping,
			BaseForm form, HttpServletRequest request,
			HttpServletResponse response, int pageNo, int pageSize)
			throws Exception {
		PickInvestSearchForm myForm = (PickInvestSearchForm) form;
		QueryClass qc = getQuery(QUERY_NAME);

		String whereStr = "1=1 ";
		String key = request.getParameter("key");
		if (key != null && key.length() > 0) {
			String[] keys = key.split(" ");
			for (int i = 0; i < keys.length; i++) {
				whereStr += " and content like '%" + keys[i] + "%' ";
			}
			whereStr = "(" + whereStr + ")";
		}
		CriteriaCallback cc = getCc(whereStr);
        if(myForm.getOrderstr()==null || "".equals(myForm.getOrderstr())){
        	myForm.setOrderFlag(1);
        	myForm.setOrderstr("crtTime");
        }        

		request.setAttribute("orderstr", myForm.getOrderstr());
        request.setAttribute("orderflag", myForm.getOrderFlag());
        OrderClazz oc =  getOc(myForm.getOrderstr(),myForm.getOrderFlag());

		Page page = new Page(getBaseManager().getCount(PickInvest.class, qc,
				myForm, myForm.isAnd(), cc), pageNo, pageSize);

		List dmList = getBaseManager().getObjects(PickInvest.class, qc, myForm,
				myForm.isAnd(), oc, cc, page);
		List vList = new ArrayList();
		for (int i = 0; i < dmList.size(); i++) {
			PickInvest pickInvest = (PickInvest) dmList.get(i);
			PickInvestView pickInvestView = new PickInvestView();
			pickInvestView.setPickInvest(pickInvest);
			vList.add(pickInvestView);
		}
		return new PageViewForward(forward(mapping), vList, page,
				getSelViewProps(VIEW_NAME), VIEW_NAME, ORDER_NAME);
	}

	private static CriteriaCallback getCc(final String whereStr) {
		return new CriteriaCallback() {
			public void execute(Criteria criteria) {
				criteria.add(Expression.sql(whereStr));
			}
		};
	}
    private OrderClazz getOc(String orderStr,int orderFlag){
    	List list = new ArrayList();   
    	if(orderStr!=null && !orderStr.equals("")){
    		OrderProp op = new OrderProp();
    		op.setPropName(orderStr);
    		op.setGroupName("pkInvestOrder");
    		if(orderFlag==1){
        		op.setAsc(false);
    		}else{
        		op.setAsc(true);
    		}
    		list.add(op);
    	}
    	return new OrderClazz(list);
    }
}
